﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DoubleH.Utility;
using Table = FCNS.Data.Table;

namespace StoreS
{
    /// <summary>
    /// DiaoBo.xaml 的交互逻辑
    /// </summary>
    public partial class DiaoBo : Window
    {
        public DiaoBo()
        {
            InitializeComponent();

            InitEvent();
        }

        Table.StoreOrderS order = null;
        public void Init(Table.StoreOrderS obj)
        {
            order = obj ?? new Table.StoreOrderS(Table.StoreOrderS.EnumFlag.调拨单) ;
            dataGridObject.InitColumns(DataTableText.仓库调拨单商品编辑);
            dataGridObject.IsSale = ProductS.GetProductS.EnumProductS.调拨单商品;
            InitOrder();
        }

        private void InitOrder()
        {
            if (order.StoreSId == -1)
                order.StoreSId = Table.StoreS.GetDefault().Id;

            uCStoreSOut.SelectedObjectId = order.StoreSId;
            dataGridObject.StoreS = uCStoreSOut.SelectedObject;

            dateTimeUpDownKaiDan.Value = order.OrderDateTime;
            if (order.StoreSId != -1)
            {
                Table.StoreOrderS sos = Table.StoreOrderS.GetObjectByRelatedOrderNO(order.OrderNO);
                if (sos != null)
                    uCStoreSIn.SelectedObjectId = sos.StoreSId;
            }

            textBoxNote.Text = order.Note;
            dataGridObject.ItemsSource = order.ProductSList;
        }

        private void InitEvent()
        {
            uCStoreSOut.SelectedObjectEvent += (ee) => { dataGridObject.StoreS = ee; dataGridObject.ItemsSource=null; };
            buttonSave.Click += (ss, ee) => { Save(); };
            buttonShenHe.Click += (ss, ee) => { ShenHe(); };
        }

        private void Save()
        {
            if (uCStoreSOut.SelectedObjectId == -1 || uCStoreSIn.SelectedObjectId == -1 || uCStoreSIn.SelectedObjectId == uCStoreSOut.SelectedObjectId)
            {
                MessageWindow.Show("仓库不能为空且不能相同");
                return;
            }

            order.ChuKu = true;
            order.Note = textBoxNote.Text;
            order.OrderDateTime = dateTimeUpDownKaiDan.Value.Value;
            //order.ProductSList = dataGridObject.ItemsSource;qqq
            order.StoreSId = uCStoreSOut.SelectedObjectId;
            Table.DataTableS.EnumDatabaseStatus result;
            if (order.Id == -1)
            {
                result = order.Insert();
                if (result == Table.DataTableS.EnumDatabaseStatus.操作成功)
                {

                    label1OrderNo.Content = order.OrderNO;

                    Table.StoreOrderS sos = new Table.StoreOrderS(Table.StoreOrderS.EnumFlag.调拨单);
                    sos.ChuKu = false;
                    sos.Note = order.Note;
                    sos.OrderDateTime = order.OrderDateTime;
                    sos.RelatedOrderNO = order.OrderNO;
                    sos.ProductSList = order.ProductSList;
                    sos.StoreSId = uCStoreSIn.SelectedObjectId;
                    sos.Insert();
                }
            }
            else
            {
                result = order.Update();
                if (result == Table.DataTableS.EnumDatabaseStatus.操作成功)
                {
                    Table.StoreOrderS sos = Table.StoreOrderS.GetObjectByRelatedOrderNO(order.OrderNO);
                    sos.Note = order.Note;
                    sos.OrderDateTime = order.OrderDateTime;
                    sos.ProductSList = order.ProductSList;
                    sos.StoreSId = uCStoreSIn.SelectedObjectId;
                    sos.Update();
                }
            }
            if (result == Table.DataTableS.EnumDatabaseStatus.操作成功)
                dataGridObject.EnableText = order._EnableText;
            else
                MessageWindow.Show(result.ToString());
        }

        private void ShenHe()
        {
            if (order.Id == -1)
            {
                MessageWindow.Show("请先保存单据");
                return;
            }

            Table.DataTableS.EnumDatabaseStatus result = order.ShenHe();
            if (result == Table.DataTableS.EnumDatabaseStatus.操作成功)
            {
                Table.StoreOrderS sos = Table.StoreOrderS.GetObjectByRelatedOrderNO(order.OrderNO);
                sos.ProductSList.ToList<Table.ProductS>().ForEach(f => f._TempPrice = order.ProductSList.ToList<Table.ProductS>().Find(f2 => f2.Id == f.Id)._TempPrice);
                sos.ShenHe();
            }
            else
                MessageWindow.Show(result.ToString());
        }
    }
}